﻿
@inject IHttpService _httpService
@inject IPopupService PopupService

@inject NavigationManager NavigationManager

<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="编辑收藏夹"/>

<AutoSaveTip TItem="EditFavoriteFolderViewModel" Item="Model" DataLoaded="DataLoaded" @ref="autoSave" />

@if (isReady == false)
{
    <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
}
else
{
    <CnGalWebSite.Components.Containers.MasaTitleContainer Title="编辑收藏夹" Icon="@IconType.Favorite.ToIconString()" Outline>
        <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Users.FolderMainCard Model="Model" Class="mb-4" />

        <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert Type="AlertTypes.Info" Class="mb-3">
            下方的编辑会实时生效
        </CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert>
        <div class="mb-4">
         <EditFavoriteObjectesTip Id="@FolderId"></EditFavoriteObjectesTip>
        </div>
        <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Buttons.SubmitButton OnSubmit="OnSubmit" Model="Model" Class="mb-4" />

    </CnGalWebSite.Components.Containers.MasaTitleContainer>
}


@code {
    [Parameter]
    public string UserId { get; set; }
    [Parameter]
    public long FolderId { get; set; }

    public EditFavoriteFolderViewModel Model { get; set; } = new EditFavoriteFolderViewModel();
    bool isReady = false;

    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    AutoSaveTip<EditFavoriteFolderViewModel> autoSave { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                Model = await _httpService.GetAsync<EditFavoriteFolderViewModel>("api/favorites/EditFavoriteFolder/" + FolderId);
                isReady = true;
                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取编辑收藏夹基本信息失败");
            }
        }
    }


    private async Task OnSubmit()
    {
        //上传
        try
        {
            //处理数据
            if (string.IsNullOrWhiteSpace(Model.Name))
            {
                await PopupService.ToastErrorAsync("必须填写收藏夹名称", "必须填写收藏夹名称");
                return;
            }


            var obj = await _httpService.PostAsync<EditFavoriteFolderViewModel, Result>("api/favorites/EditFavoriteFolder", Model);
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("保存收藏夹基本信息失败", obj.Error);
            }
            else
            {
                await autoSave.Clean();

                await PopupService.ToastSuccessAsync("保存收藏夹基本信息成功", "保存收藏夹基本信息成功");

                if (string.IsNullOrWhiteSpace(UserId))
                {
                    NavigationManager.NavigateTo("/favoritefolders/index/" + FolderId);

                }
                else
                {
                    NavigationManager.NavigateTo("/space/EditSpaceFavoriteFolders/" + UserId);
                }


            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "保存收藏夹基本信息失败");
        }
    }
    private void DataLoaded(EditFavoriteFolderViewModel model)
    {
        isReady = false;
        StateHasChanged();
        Model = model;
        isReady = true;
        StateHasChanged();
        
    }

}
